package oj0413.oj0413_04;

import java.util.Scanner;

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
    }
}

class LinkList {
    private Node p;

    public LinkList(int[] a, int n, int m) {
        if (n == 0) {
            p = null;
            return;
        }
        Node first = new Node(a[0]);
        Node rear = first;
        for (int i = 1; i < n; i++) {
            Node s = new Node(a[i]);
            rear.next = s;
            rear = s;
            if (i < m) {
                p = s;
            }
        }
        rear.next = first;
    }

    public void delete() {
        //自己补充代码
        if (p == null || p.next == p) {
            return;
        }
        Node pre = p;   //找p的前驱
        Node n = p.next;  //前驱
        while (n.next != p){
            pre = n;
            n = n.next;
        }
        pre.next = p;  //重新指向
    }

    public void display() {
        if (p == null) {
            return;
        }
        Node q = p;
        while (q.next != p) {
            System.out.print(q.data + "->");
            q = q.next;
        }
        System.out.println(q.data);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        LinkList link = new LinkList(a, n, m);
        link.delete();
        link.display();
        scanner.close();
    }
}
